<?php
namespace app\admin\controller;
use app\admin\logic\GoodsLogic;
use app\admin\logic\SearchWordLogic;
use think\AjaxPage;
use think\Loader;
use think\Page;
use think\Db;

class Distribut extends Base {

    public function rebate_log(){


        $mobile    = I('mobile');
        $order_sn   = I('order_sn');
        $start_time = I('start_time');
        $end_time   = I('end_time');
        $status     = I('status');

        $user_info = M('users')->where('mobile',$mobile)->find();
        $user_id = $user_info['user_id'];


        if($start_time > 0  && $end_time === 0){
            $where = " create_time >= '".strtotime($start_time)."' ";
        }elseif($start_time === 0  && $end_time > 0){
            $where = " create_time <= '".strtotime($end_time)."' ";
        }elseif($start_time > 0  && $end_time > 0){
            $where = " create_time >= '".strtotime($start_time)."' and create_time <= '".strtotime($end_time)."' ";
        }else{
            $where = ' 1=1 ';
        }

        if($status === '0' || $status > 0)
            $where .= " and status = $status ";
        $user_id && $where .= " and user_id = $user_id ";
        $order_sn && $where .= " and order_sn like '%$order_sn%' ";

        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('mobile', $mobile);
        $this->assign('order_sn', $order_sn);

//echo $where;
        $count = M('rebate_log')->where($where)->count();
        $Page  = new Page($count,10);
        $show  = $Page->show();

        $list = M('rebate_log')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $key=>$val){
            //获佣用户
            $userinfo = M('users')->where('user_id',$val['user_id'])->find();
            $list[$key]['fl_nickname'] = $userinfo['nickname'];
            $list[$key]['fl_email'] = $userinfo['email'];
            $list[$key]['fl_mobile'] = $userinfo['mobile'];
            $list[$key]['dislevel'] = $userinfo['distribution'];

            //购买用户
            $userinfo = M('users')->where('user_id',$val['buy_user_id'])->find();
            $list[$key]['nickname'] = $userinfo['nickname'];
            $list[$key]['email'] = $userinfo['email'];
            $list[$key]['mobile'] = $userinfo['mobile'];
        }

        $this->assign('show',$show);
        $this->assign('pager',$Page);

        $this->assign('list',$list);
        return $this->fetch();
    }
    public function tree(){

        return $this->fetch();
    }
    public function distributor_list(){

        $where = ' 1=1';
        $mobile = I('mobile');

        if($mobile){
            $mobile && $where .= " and mobile = $mobile ";
        }

        $where .= " and is_distribut = 1";

        $count = M('users')->where($where)->count();
        $Page  = new Page($count,10);
        $show  = $Page->show();
        $user_list = M('users')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('mobile',$mobile);
        $this->assign('show',$show);
        $this->assign('pager',$Page);

        $this->assign('user_list',$user_list);
        return $this->fetch();
    }

    //申请成为合伙人，审核界面
    public function vdistributor(){
        // 搜索条件
        $condition = array();
        I('mobile') ? $condition['mobile'] = I('mobile') : false;
        I('realname') ? $condition['realname'] = I('realname') : false;

//        $sort_order = I('order_by').' '.I('sort');
        $sort_order = ' add_time desc';

        $model = M('user_dapply');
        $count = $model->where($condition)->count();
        $Page  = new AjaxPage($count,10);
        //  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }

        $userapplyList = $model->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach($userapplyList as $key=>$val){
            $userinfo  = M('users')->where('user_id',$val['user_id'])->find();
            $userapplyList[$key]['nickname'] = $userinfo['nickname'];
            $userapplyList[$key]['head_pic'] = $userinfo['head_pic'];
        }

//        dump($userapplyList);

        $show = $Page->show();
        $this->assign('userapplyList',$userapplyList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }

    //审核通过，修改用户资料
    public function applySuccess(){
        $user_id = I('user_id');
        $applyInfo = M('user_dapply')->where('user_id',$user_id)->find();

        $distribution_value = M('user_dislevel')->where('dislevel_id','1')->find();

        $date = array(
            'mobile' => $applyInfo['mobile'],
            'realname' => $applyInfo['realname'],
            'is_distribut' => 1,
            'first_leader' => 0,//审核成功后，解除上下级关系，8月5日和小莲确认的。

            'distribution'  => 1,//初始比例，edit by chb
            'distribution_value'  => $distribution_value['discount'],//初始比例，edit by chb
        );

        if(M('users')->where(array('user_id' => $user_id))->save($date)){
            M('user_dapply')->where(array('user_id' => $user_id))->save(array('status'=>1));
            $this->success('审核成功');
        }else{
            $this->success('审核失败');
        }


    }


}
